module.exports = {
	/**
	 * 修改数据
	 * @url agent/toufang/kh/update 前端调用的url参数地址
	 * data 请求参数 说明
	 * res 返回参数说明
	 * @params {Number} code 错误码，0表示成功
	 * @params {String} msg 详细信息
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid
		} = data;
		let res = {
			code: 0,
			msg: 'ok'
		};
		// 业务逻辑开始-----------------------------------------------------------
		// 获取前端传过来的参数
		let {
			_id,
			bind_first_recharge,
			bind_first_recharge_id,
			bind_again_recharge_id,
			bind_again_recharge,
			ad_id,
			desc,
			tfurl,
			middleman_id,
			agentId,
			agent_username,
			appid,
			appname,
			tv_id,
			tv_name,
			series,
			follow,
			gzhId,
			path,
			album_id,
			tfid,
			platform
		} = data;
		// 参数验证开始
		if (vk.pubfn.isNull(_id)) return {
			code: -1,
			msg: 'id不能为空'
		};

		//校验一下填写的appid有没有对应的秘钥存在数据库里，没有的话就修改失败
		let appinfo = await db.collection('a_appid').where({
			appid: appid
		}).get()
		if (appinfo.data.length == 0) {
			res.code = 1
			res.msg = "未找到appid对应的appsecret,请先去小程序配置--小程序管理添加"
			return res;
		}
		
		// 根据aid series 查询 eid
		let epid = await vk.baseDao.select({
			dbName: "a_tv_series", // 表名
			whereJson: { // 条件
				series:series,
				album_id:album_id
			},
		});
		console.log("epid80===",epid.rows[0].episode_id);
		let episode_id = epid.rows[0].episode_id

		const purl = "pages/index/index?path=player&tv_id=" + tv_id + "&series=" + series +
			"&invite_code=" + agentId + "&tfid=" + _id + "&middleman_id=" + middleman_id +
			"&tt_album_id=" + album_id + "&tt_episode_id=" + episode_id;

		console.log("purl===", purl);

		// 参数验证结束
		let dbName = "a_dy_tfurl"; // 表名
		// 执行 数据库 updateById 命令
		await vk.baseDao.updateById({
			dbName,
			id: _id,
			dataJson: {
				ad_id,
				bind_first_recharge,
				bind_first_recharge_id,
				bind_again_recharge_id,
				bind_again_recharge,
				desc,
				tfurl,
				xcxpath: "pages/index/index?path=player&tv_id=" + tv_id + "&series=" + series +
					"&invite_code=" + agentId + "&tfid=" + _id + "&middleman_id=" + middleman_id +
					"&tt_album_id=" + album_id + "&tt_episode_id=" + episode_id,
				agentId,
				agent_username,
				appid,
				appname,
				tv_id,
				tv_name,
				series,
				follow,
				gzhId,
				path,
				tfid,
				platform
			}
		});

		await vk.baseDao.update({
			dbName: "a_agent_settingTV", // 表名
			whereJson: { // 条件
				tfid: _id,
			},
			dataJson: { // 需要修改的数据
				tv_id: tv_id,
				tv_name: tv_name
			}
		});


		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}

}